#include "Bot.h"

Bot::Bot()
{
	CurrentState = NULL;
	BotShip = NULL;
}

Bot::Bot(IDirect3DDevice9 *nDevice, float nMass, State *nState, D3DXVECTOR3 botSize, float nThrust)
{
	BehaviorID = 0;
	DesiredAngles = D3DXVECTOR3(0,0,0);
	IsTurningOvertime = false;
	MaxThrottle = 100;
	CurrentState = nState;
	if(CurrentState)
	{
		CurrentState->SetParent(this);
	}
	BotShip = new TestShip(nDevice, nMass, botSize);
	BotShip->InitThrusters(nThrust);
	MaxRotation = 90.f;

	//TEST CODE//
	/*BotShip->SetPosition(D3DXVECTOR3(20,20,20));
	BotShip->SetAngles(D3DXVECTOR3(0,0,0));*/
	//BotShip->SetForwardThrottle(10);

}

Bot::~Bot()
{
	if(CurrentState)
		delete CurrentState;
	if(BotShip)
		delete BotShip;
}

bool Bot::ChangeState( State *nState)
{
	if(CurrentState)
	{
		CurrentState->Exit();
		delete CurrentState;
	}

	CurrentState = nState;
	CurrentState->SetParent(this);
	CurrentState->Enter();
	return true;
}

void Bot::Update(float dt)
{
	CurrentState->Execute(dt);
	IsTurningOvertime = false;

	D3DXVECTOR3 FinalAngles = DesiredAngles - BotShip->GetAngles();
	float TempMaxRot = MaxRotation * dt;

	//D3DXVec3Normalize(&FinalAngles, &FinalAngles);

	if(FinalAngles.x > DegToRad(TempMaxRot))
	{
		FinalAngles.x = DegToRad(TempMaxRot);
		IsTurningOvertime = true;
	}
	if(FinalAngles.x < DegToRad(-TempMaxRot))
	{
		FinalAngles.x = DegToRad(-TempMaxRot);
		IsTurningOvertime = true;
	}


	if(FinalAngles.y > DegToRad(TempMaxRot))
	{
		FinalAngles.y = DegToRad(TempMaxRot);
		IsTurningOvertime = true;
	}
	if(FinalAngles.y < DegToRad(-TempMaxRot))
	{
		FinalAngles.y = DegToRad(-TempMaxRot);
		IsTurningOvertime = true;
	}

	if(FinalAngles.z > DegToRad(TempMaxRot))
	{
		FinalAngles.z = DegToRad(TempMaxRot);
		IsTurningOvertime = true;
	}
	if(FinalAngles.z < DegToRad(-TempMaxRot))
	{
		FinalAngles.z = DegToRad(-TempMaxRot);
		IsTurningOvertime = true;
	}

	BotShip->SetAngles(BotShip->GetAngles()+FinalAngles);

	if(IsTurningOvertime)
		BotShip->SetForwardThrottle(short(MaxThrottle * .25));
	else
		BotShip->SetForwardThrottle(MaxThrottle);//*/


	//BotShip->SetAngles(FinalAngles);
	/*if(BotShip->GetPosition().z < -1000)
		BotShip->SetForwardThrottle(100);
	if(BotShip->GetPosition().z > 1000)
		BotShip->SetBackwardThrottle(100);*/
	//BotShip->SetForwardThrottle(0);
	BotShip->Update(dt);
}

void Bot::Render( void )
{
	if(BotShip)
		BotShip->Render();
}

void Bot::SetEffect(ID3DXEffect *nEffect)
{
	if(BotShip)
		BotShip->SetEffect(nEffect);
}

void Bot::SetWVPMat(D3DXMATRIX nWVP)
{
	if(BotShip)
		BotShip->SetWVPMat(nWVP);
}

void Bot::SetEyePosition(const D3DXVECTOR3 &nVector)
{
	BotShip->SetEyePosition(nVector);
}

void Bot::SetTarget(Entity *nState)
{
	CurrentState->SetTarget(nState);
}

D3DXVECTOR3 Bot::GetAngles( void )
{
	return BotShip->GetAngles();
}

D3DXVECTOR3 Bot::GetDesiredAngles( void )
{
	return DesiredAngles;
}

void Bot::SetDesiredAngles(D3DXVECTOR3 nDAngles)
{
	DesiredAngles = nDAngles;
}

void Bot::SetMaxThrottle(short nMaxThrottle)
{
	MaxThrottle = nMaxThrottle;
}

bool Bot::GetIsTurningOvertime( void )
{
	return IsTurningOvertime;
}

void Bot::SetBehaviorID( UINT nID )
{
	BehaviorID = nID;
}

UINT Bot::GetBehaviorID( void )
{
	return BehaviorID;
}

#ifdef _DEBUG
void Bot::DebugOutput(ID3DXFont *DebugFont)
{
	std::ostringstream ConvString;

	ConvString << "Direction of Player: \n"
			   << "X: "<< DebugVec.x 
			   << ", Y: " << DebugVec.y
			   << ", Z: " << DebugVec.z
			   << "\nBot Throttle: " << BotShip->GetThrottle()
			   << "\nBot Speed: " << (D3DXVec3Length(&BotShip->GetVelocity()))
			   << "\nBot Position: " << "X: " << BotShip->GetPosition().x
			   << ", Y: " << BotShip->GetPosition().y
			   << ", Z: " << BotShip->GetPosition().z;

	ConvString <<"\n\nBot's Bounding Box: " 
			   <<"X: " << BotShip->BoundingBox.GetHalfVector().x
			   <<", Y: " << BotShip->BoundingBox.GetHalfVector().y
			   <<", Z: " << BotShip->BoundingBox.GetHalfVector().z;
	RECT rect;
	rect.top = 600;
	rect.bottom = 900;
	rect.left = 25;
	rect.right = 1600;
	DebugFont->DrawTextA(NULL, ConvString.str().c_str(), -1, &rect, NULL, D3DXCOLOR(255,255,255,255));

}
#endif